package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private TextView lview;
    private ListView listView;
    private String oid,name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        lview = findViewById(R.id.tv_login_1);
        listView = (ListView) findViewById(R.id.list_view);
        Bundle bundle=getIntent().getExtras();
        name=bundle.getString("name");
        oid=bundle.getString("oid");
        lview.setText("欢迎,"+name);
        getinit();
    }
    protected void getinit(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Intent intent=null;
                BufferedReader buf=null;
                String result;
                HttpURLConnection logincon = null;
                String url="http://10.128.48.203:8000/organization?oid="+oid;
                try{
                    URL requesturl= new URL(url);
                    logincon=(HttpURLConnection)requesturl.openConnection();
                    logincon.setRequestMethod("GET");
                    logincon.setReadTimeout(5000);
                    logincon.connect();

                    if(logincon.getResponseCode()==200){
                        //获取二进制字节流
                        InputStream inputs=logincon.getInputStream();

                        //将二进制流包装
                        buf =new BufferedReader(new InputStreamReader(inputs));

                        //读取字符串
                        String line;
                        StringBuilder builder = new StringBuilder();
                        while((line=buf.readLine())!=null){
                            builder.append(line);
                            builder.append("\n");
                        }
                        result=builder.toString();
                        JSONObject jsonObject = new JSONObject(result);
                        JSONArray organs=jsonObject.getJSONArray("sub_organizations");
                        String soid,soname;
                        List<Suborg> suborgList = new ArrayList<>();
                        for(int i=0;i<organs.length();i++){
                            JSONObject sub=organs.optJSONObject(i);
                            soid=sub.optString("id");
                            soname=sub.optString("name");
                            Suborg sss = new Suborg(soname,soid);
                            suborgList.add(sss);
                        }
                        SuborgAdapter suborgAdapter= new SuborgAdapter(LoginActivity.this,R.layout.subo_layout,suborgList);
                        listView.setAdapter(suborgAdapter);
                        //创建点击事件
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                                Suborg suborg= suborgList.get(position);
                                String sid=suborg.soid;
                                Intent intent1= new Intent(LoginActivity.this,SubOrganActivity.class);
                                Bundle bundle =new Bundle();
                                bundle.putString("soid",sid);
                                bundle.putString("name",name);
                                intent1.putExtras(bundle);
                                startActivity(intent1);
                            }
                        });
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
}